/**
 * @author smallwang
 * @email justdoalitter@gmail.com
 * @create date 2025-01-31 16:17:10
 * @modify date 2025-01-31 16:17:10
 * @desc [description]
 */
package ltd.smallwang;

import ltd.smallwang.common.ListNode;

public class Leetcode0021 {
  public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
    return method2(list1, list2);
  }

  @SuppressWarnings("unused")
  private ListNode method1(ListNode list1, ListNode list2) {
    ListNode headNode = new ListNode(0);
    ListNode currNode = headNode;
    while (list1 != null && list2 != null) {
      if (list1.val < list2.val) {
        currNode.next = list1;
        list1 = list1.next;
      } else {
        currNode.next = list2;
        list2 = list2.next;
      }
      currNode = currNode.next;
    }
    if (list1 != null) {
      currNode.next = list1;
    }
    if (list2 != null) {
      currNode.next = list2;
    }
    return headNode.next;
  }

  private ListNode method2(ListNode list1, ListNode list2) {
    if (list1 == null) {
      return list2;
    }
    if (list2 == null) {
      return list1;
    }
    if (list1.val < list2.val) {
      list1.next = method2(list1.next, list2);
      return list1;
    } else {
      list2.next = method2(list1, list2.next);
      return list2;
    }
  }
}
